var clip_rect = null;

/**
 * 清除图片
 * @param x
 * @param y
 * @param width
 * @param height
 */
function clear_img(x, y, width, height) {
	var canvas = document.getElementById("canvas_content");
	var context = canvas.getContext("2d");
	var arc = (arguments[4]) ? arguments[4] : 0;
	context.save();
	context.rotate(arc / 180 * Math.PI);
	context.translate(x, y)
	context.clearRect(0, 0, width, height);
	context.restore();
}

/**
 * 绑定图片移动处理事件
 */
function move_img_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	
	$("canvas").mousedown(function(event) {
		if(judge_in_img()) {
			$("canvas").mouseup(function(event) {
				redraw_move_img(event);
				move_x = 0; //记录上一个鼠标所在坐标点
				move_y = 0;
				$("canvas").unbind("mouseup");
				$("canvas").unbind("mousemove");
			})
			
			$("canvas").mousemove(function(event) {
				redraw_move_img(event);
			})
		} else {
			move_x = 0;
			move_y = 0;
		}
	})
	
}

/**
 * 图片移动时的重绘
 * @param event
 */
function redraw_move_img(event) {
	var offset = $("canvas").offset();
	var x = Math.floor(event.pageX - offset.left);
	var y = Math.floor(event.pageY - offset.top);
	
	var img = images[img_index];
	var new_x = img.getStart_X() + x - move_x;
	var new_y = img.getStart_Y() + y - move_y;
	var new_left_x = img.getLeft_X() + x - move_x;
	var new_left_y = img.getLeft_Y() + y - move_y;
	
	
	clear_img(img.getStart_X(), img.getStart_Y(), img.getWidth(), img.getHeight(), img.getArc());
	img.setStart_X(new_x);
	img.setStart_Y(new_y);
	img.setLeft_X(new_left_x);
	img.setLeft_Y(new_left_y);

	redraw_all();
	move_x = x;
	move_y = y;
}

/**
 * 绑定图片缩放处理事件
 */
function scale_img_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	
	$("canvas").mousedown(function(event) {
		if(judge_in_img()) {
			$("canvas").mouseup(function(event) {
				redraw_scale_img(event);
				move_x = 0;
				move_y = 0;
				$("canvas").unbind("mouseup");
				$("canvas").unbind("mousemove");
			})
			
			$("canvas").mousemove(function(event) {
				redraw_scale_img(event);
			})
		} else {
			move_x = 0;
			move_y = 0;
		}
	})
}

/**
 * 图片缩放时的重绘
 * @param event
 */
function redraw_scale_img(event) {
	var offset = $("canvas").offset();
	var x = Math.floor(event.pageX - offset.left);
	var y = Math.floor(event.pageY - offset.top);
	
	var img = images[img_index];
	var new_h = null;
	var new_v = null;
	
	var arc = img.getArc();
	
	clear_img(img.getStart_X(), img.getStart_Y(), img.getWidth(), img.getHeight(), arc);
	
	if((arc / 90) % 4 == 0) {
		new_h = img.getHorizontal()+ x - move_x;
		new_v = img.getVertical() + y - move_y;
		if(new_h > 0 && new_v > 0) {
			img.setWidth(new_h);
			img.setHeight(new_v);
		}		
	} else if ((arc / 90) % 4 == 1) {
		new_h = img.getHorizontal()+ move_x - x;
		new_v = img.getVertical() + y - move_y;
		if(new_h > 0 && new_v > 0) {
			img.setWidth(new_v);
			img.setHeight(new_h);
		}
	} else if ((arc / 90) % 4 == 2) {
		new_h = img.getHorizontal()+ move_x - x;
		new_v = img.getVertical() + move_y - y;
		if(new_h > 0 && new_v > 0) {
			img.setWidth(new_h);
			img.setHeight(new_v);
		}
	} else if((arc / 90) % 4 == 3) {
		new_h = img.getHorizontal()+ x - move_x;
		new_v = img.getVertical() + move_y - y;
		if(new_h > 0 && new_v > 0) {
			img.setWidth(new_v);
			img.setHeight(new_h);
		}
	}
	
	img.setHorizontal(new_h);
	img.setVertical(new_v);
 	
	redraw_all();
	move_x = x;
	move_y = y;
}


function rotate_img_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	
	$("canvas").mousedown(function(event) {
		if(judge_in_img()) {
			$("canvas").mouseup(function(event) {
				redraw_rotate_img(event);
				move_x = 0;
				move_y = 0;
				$("canvas").unbind("mouseup");
				$("canvas").unbind("mousemove");
			})
		} else {
			move_x = 0;
			move_y = 0;
		}
	})
}


function redraw_rotate_img(event) {
	var offset = $("canvas").offset();
	var x = Math.floor(event.pageX - offset.left);
	var y = Math.floor(event.pageY - offset.top);
	
	var img = images[img_index];
	
	var start_x = img.getStart_X();
	var start_y = img.getStart_Y();
	var width = img.getWidth();
	var height = img.getHeight();

	clear_img(start_x, start_y, width, height, img.getArc());
	
	var arc = img.getArc();
	arc += 90;
	img.setArc(arc);
	
	var left_x = null;
	var left_y = null;
	
	if((arc / 90) % 4 == 0) {
		left_x = start_x;
		left_y = start_y;
		img.setHorizontal(width);
		img.setVertical(height);
	} else if ((arc / 90) % 4 == 1) {
		left_x = start_x - height;
		left_y = start_y;
		img.setHorizontal(width);
		img.setVertical(height);
	} else if ((arc / 90) % 4 == 2) {
		left_x = start_x - width;
		left_y = start_y - height;
		img.setHorizontal(width);
		img.setVertical(height);
	} else if((arc / 90) % 4 == 3) {
		left_x = start_x;
		left_y = start_y - width;
		img.setHorizontal(width);
		img.setVertical(height);
	}
	
	img.setLeft_X(left_x);
	img.setLeft_Y(left_y);
	
	redraw_all();
}


/**
 * 判断鼠标是否落在图片区域内
 * @returns {Boolean}
 */
function judge_in_img() {
	var offset = $("canvas").offset();
	var x = Math.floor(event.pageX - offset.left);
	var y = Math.floor(event.pageY - offset.top);

	var img = images[img_index];
	var left_x = img.getLeft_X();
	var left_y = img.getLeft_Y();
	var image = img.getImage();
	var end_x = left_x + img.getHorizontal();
	var end_y = left_y + img.getVertical();
	
	var judge_x = (left_x <= x) && (x <= end_x);
	var judge_y = (left_y <= y) && (y <= end_y);
	
	move_x = x;
	move_y = y;
	
	return (judge_x && judge_y);
}

function init_img_item(index) {
	var file = null;
	var strs = null;
	$(".select_content").each(function() {
		file = images[index].getImage().src;
		strs = file.split("/");
		file = strs[strs.length - 1];
		$("<option>" + file + "</option>").attr('value', index).appendTo(this);
	});
}


function clip_canvas_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	
	$("canvas").mousedown(function() {
		var offset = $("canvas").offset();
		var x = Math.floor(event.pageX - offset.left);
		var y = Math.floor(event.pageY - offset.top);
		clip_rect = new Rectangle();
		clip_rect.setStart_X(x);
		clip_rect.setStart_Y(y);
		clip_rect.setBegin_X(x);
		clip_rect.setBegin_Y(y);
		clip_rect.setWidth(0)
		clip_rect.setHeight(0);
		
		if(judge_in_img()) {
			$("canvas").mousemove(function(event) {
				redraw_clip_rect(event);
			});	
		
			$("canvas").mouseup(function(event) {
				redraw_clip_rect(event);
				clip_canvas(event);
				$("canvas").unbind("mouseup");
				$("canvas").unbind("mousemove");
			});
		}
			
	})
}

function redraw_clip_rect(event) {
	var offset = $("canvas").offset();
	var end_x = Math.floor(event.pageX - offset.left);
	var end_y = Math.floor(event.pageY - offset.top);
	clip_rect.setColor("#FFFFFF");
	clip_rect.draw();
	var start_x = clip_rect.getStart_X();
	var start_y = clip_rect.getStart_Y();
	var x = (start_x > end_x) ? end_x : start_x;
	var y = (start_y > end_y) ? end_y : start_y;
	var width = Math.abs(start_x - end_x);
	var height = Math.abs(start_y - end_y);
	clip_rect.setBegin_X(x);
	clip_rect.setBegin_Y(y);
	clip_rect.setWidth(width);
	clip_rect.setHeight(height);
	var color = $("#current_color").css('background-color');
	clip_rect.setColor(color);
	redraw_all();
	clip_rect.draw();
}

function clip_canvas(event) {
	var x = clip_rect.getBegin_X();
	var y = clip_rect.getBegin_Y();
	var width = clip_rect.getWidth();
	var height = clip_rect.getHeight();
	var canvas = document.getElementById("canvas_content");
	var context = canvas.getContext("2d");

	var img = images[img_index];
	clear_img(img.getStart_X(), img.getStart_Y(), img.getWidth(), img.getHeight(), img.getArc());
	context.save();
	context.rect(x, y, width, height);
	context.clip();
	img.draw();
	
}

